package com.citywithincity.db.sync.impl;

import java.text.SimpleDateFormat;

import com.citywithincity.api.ApiBuildConfig;
import com.citywithincity.db.Db;
import com.citywithincity.db.interfaces.Ar;
import com.citywithincity.db.sync.SyncCondition;
import com.citywithincity.utils.DataUtil;

/**
 * oracle最大date，查询
 * @author renxueliang
 *
 */
public class OracleMaxDate implements SyncCondition {
	
	/*
	 * 日期字段
	 */
	private String dateField;
	private String targetTable;
	private String targetFeild;
	private long defaultInterval;
	/**
	 * 
	 * @param dateField			源表日期字段
	 * @param targetTable		目标表名称
	 * @param targetFeild		目标字段
	 * @param defaultInterval	默认时间间隔（毫秒)
	 */
	public OracleMaxDate(String dateField,String targetTable,String targetFeild,long defaultInterval){
		this.dateField = dateField;
		this.targetTable = targetTable;
		this.targetFeild = targetFeild;
		this.defaultInterval = defaultInterval;
	}
	
	private long maxRk;
	
	@Override
	public void setAr(Ar ar) {
		if(maxRk==0){
			maxRk = System.currentTimeMillis() - defaultInterval;
		}
		ar.orderBy(dateField,Ar.ASC);
		ar.where(String.format("%s>=TO_DATE('%s','yyyymmddhh24miss')",
				dateField,new SimpleDateFormat(ApiBuildConfig.SHORT_DATE_TIME_FORMAT)
				.format(new java.util.Date(maxRk))));
	}

	@Override
	public void init() {
		//取得本表的最大时间
		maxRk = DataUtil.toLong(Db.ar().selectMax(this.targetTable, this.targetFeild)) * 1000;
		//取得一个月的数据
		
	}
}
